package com.example.cola.domain.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.regex.Pattern;

/**
 * 邮箱值对象
 * 
 * @author 资深开发专家
 * @version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Email {
    
    private String value;
    
    // 邮箱正则表达式
    private static final Pattern EMAIL_PATTERN = Pattern.compile(
        "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
    );
    
    public static Email of(String email) {
        if (email == null || email.trim().isEmpty()) {
            throw new IllegalArgumentException("邮箱不能为空");
        }
        
        String trimmedEmail = email.trim().toLowerCase();
        if (!EMAIL_PATTERN.matcher(trimmedEmail).matches()) {
            throw new IllegalArgumentException("邮箱格式不正确");
        }
        
        return new Email(trimmedEmail);
    }
    
    @Override
    public String toString() {
        return value;
    }
}
